package com.ssm.demo.utils;

import com.ssm.demo.pojo.VO.ResultVO;
import lombok.Getter;

public class R {

    // 响应状态枚举
    @Getter
    public enum ResponseStatus {
        SUCCESS(1, "成功"),
        UPLOAD_SUCCESS(1,"上传成功"),
        FAILURE(0,"失败"),
        PARAMETER_ERROR(0,"参数错误");

        private final int code;
        private final String message;

        ResponseStatus(int code, String message) {
            this.code = code;
            this.message = message;
        }

        /**
         * 根据状态码获取响应状态对象
         * @param code 状态码
         * @return 匹配的ResponseStatus枚举对象，如果没有匹配的，则返回null
         */
        public static ResponseStatus fromCode(int code) {
            for (ResponseStatus status : values()) {
                if (status.getCode() == code) {
                    return status;
                }
            }
            return null;
        }
    }

    private R(){}

    // 示例方法：创建一个成功的响应实体
    public static <T> ResultVO<T> successR(T data) {
        return new ResultVO<>(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), data);
    }

    // 示例方法：创建一个错误的响应实体
    public static <T> ResultVO<T> errorR(ResponseStatus status) {
        return new ResultVO<>(status.getCode(), status.getMessage(), null);
    }
}
